<?php
## Dependencias
require_once($GLOBALS['PAD'] . "Aplications/ReportModels/report.vars.php");
include($GLOBALS['OBJ'] . 'obj.mathgrids.mex');

MYTCONN();
ini_set('max_execution_time', 100);

## Variaveis do Report
$PrintResults = '';
$ViewType = $_POST['viewtype'];
if ($ViewType == 1) {
    if (!isset($_POST['ConsultDate'])) {
        $_POST['ConsultDate'] = date("Y-m-d");
    }
    $DateStart = $_POST['ConsultDate'];
    $DateEnd = $_POST['ConsultDate'];
    $viewloop = 2;
} elseif ($ViewType == 2) {
    $DateStart = $_POST['Tini'];
    $DateEnd = $_POST['Tend'];
    $viewloop = 1;
}

$Country = 0;
$Origens = 1;
$Channel = 1;
if (isset($_POST['Countries']) && $_POST['Countries']>0) {$Country = $_POST['Countries'];}
if (isset($_POST['Origens'])) {$Origens = $_POST['Origens'];}
if (isset($_POST['Channel'])) {$Channel = $_POST['Channel'];}
if($Channel<1){$Channel=0;}

#Verifica
if($DateEnd < $DateStart){die("<script type=\"text/javascript\">alert('A data de fim não pode ser inferior à data de inicio.');</script>");}

$QrCountry = '';
$QrChannel = '';
$RsProfit = '';
$ExtendQuery = '';



if ($Channel > 0) { $QrChannel = ' AND FraModo = ' . $Channel;}
if ($Country > 0) { $QrCountry = " AND pai_Profit = $Country";}
$RsProfit = " AND RsProfit = $Country";

$GETMIN = $GLOBALS['MYT']->db_array("SELECT FraID AS QR FROM facturas
		WHERE YEAR(FraFecha) = YEAR('{$DateStart}') AND MONTH(FraFecha) = MONTH('{$DateStart}') ORDER BY FraID ASC LIMIT 0,1");
$GETMAX = $GLOBALS['MYT']->db_array("SELECT FraID AS QR FROM facturas
		WHERE YEAR(FraFecha) = YEAR('{$DateEnd}') AND MONTH(FraFecha) = MONTH('{$DateEnd}') ORDER BY FraID DESC LIMIT 0,1");
if (!isset($GETMIN[1])) {
    $GETMIN = 0;
} else {
    $GETMIN = $GETMIN[1]['QR'];
}
if (!isset($GETMAX[1])) {
    $GETMAX = 0;
} else {
    $GETMAX = $GETMAX[1]['QR'];
}


## Set Fields
$SentAmount[1] = 'FraEnviado+FraProm';
$PaidAmount[1] = 'FraARecibir*CorrRate';
$FeesAmount[1] = 'FraCargo';
$SentAmount[2] = 'FraARecibir';
$PaidAmount[2] = 'FraARecibir';
$FeesAmount[2] = 'FraIncComisionCo';

$NoResults = '<tr bgcolor="#F2F2F2"><td colspan="14">Não existem resultados para apresentação.</td></tr>';

$head = '<tr style="background-color:#C7D3D8;height:18px;text-align:center">
	<td NOWRAP width="17%"></td>
	<td NOWRAP width="7%">#</td>
	<td NOWRAP width="9%" colspan="';

if ($Country == 2) {
    $head .= '4';
} else {
    $head .= '3';
}
$head .= '">Valores</td>
	<td NOWRAP width="10%" colspan="3">Lucro Bruto</td>
	<td NOWRAP width="10%" colspan="3">Por remessa</td>
	<td NOWRAP width="10%">Por funcionário</td>
	<td NOWRAP width="10%">Novos</td>';
if ($Country == 0) {
    $head .= '<td NOWRAP width="8%" colspan="2">Lucro Liquido</td>';
} else {
    $head .= '<td NOWRAP width="8%" colspan="3">Meta</td>';
}

$head .= '</tr>';

### Sub-title
$head .= '<tr style="background-color:#D7E3E8;height:18px;text-align:center">
	<td width="12%">Lojas</td>
	<td width="7%">Remessas</td>
	<td width="7%">Lote</td>';
if ($Country == 2) {
    $head .= '<td width="7%">Co.Money</td>
	<td width="7%">Co.Corr</td>
	<td width="7%">Imp.Selo</td>';
} else {
    $head .= '<td width="7%">Taxas</td>';
    $head .= '<td width="7%">Promocoes</td>';
}
$head .= '<td width="7%">Lote</td>
	<td width="7%">Total</td>
	<td width="7%">%</td>';
if ($Country == 2) {
    $head .= '<td width="7%">Comissão</td>';
} else {
    $head .= '<td width="7%">Taxa</td>';
}
$head .= '<td width="7%">Lucro</td>
	<td width="7%">Valor Médio</td>
	<td width="7%">Remessas</td>
	<td width="7%">Clientes</td>';
if ($Country == 0) {
    $head .= '<td width="7%">Despesa</td>
	<td width="7%">Liquido</td>';
} else {
    $head .= '<td NOWRAP width="6%">Valor</td>
	<td NOWRAP width="6%">Em Falta</td>
	<td NOWRAP width="6%">% Cumprido</td>';
}
$head .= '</tr>';

$PrintResults .= $tablehead;

for ($l = 1; $l <= $viewloop; $l++) {

    if ($l == 1) {

        $ConsultDay = substr($DateStart, 8, 2);
        $ConsultMonth = substr($DateStart, 5, 2);
        $ConsultYear = substr($DateStart, 0, 4);
        $RateField = $_POST['RelRate'];

        if ($Origens == 1) {
            $QrOrigens = 'FraOrigen0';
            $QrRowName = 'main_offices.name';
        } elseif ($Origens == 2) {
            $QrOrigens = 'FraOrigen1';
            $QrRowName = 'OriNombre';
        } elseif ($Origens == 3) {
            $QrOrigens = 'OriTipo';
            $QrRowName = 'OriTipo';
        } elseif ($Origens == 4) {
            $QrOrigens = 'dom_Region';
            $QrRowName = 'dom_Region';
        }
        $currency = '';
        if ($ViewType == 1) {
            $PrintResults .= '<tr><td colspan="20"><div style="font-weight:bold;text-align:center;border:1px solid #CCC;margin:2px;padding:3px;background-color:#F2F2F2">Resumo diário de lucro por lojas do dia ' . $ConsultDay . '-' . $ConsultMonth . '-' . $ConsultYear . '</div></td></tr>';
        } elseif ($ViewType == 2) {
            $PrintResults .= '<tr><td colspan="20"><div style="font-weight:bold;text-align:center;border:1px solid #CCC;margin:2px;padding:3px;background-color:#F2F2F2">Resumo por lojas no periodo entre ' . $DateStart . ' e ' . $DateEnd . '</div></td></tr>';
        }
        $PrintResults .= $head;
        $TotalSumWorkers = 0;

        $QrStrDate[1] = "FraFecha BETWEEN '$DateStart 00:00:00' AND '$DateEnd 23:59:59'";
        $QrStrDateClient[1] = "CliFechaAlta BETWEEN '$DateStart 00:00:00' AND '$DateEnd 23:59:59'";

    } elseif ($l == 2) {

        $Interv = $GLOBALS['MYT']->db_array("SELECT
	MAX(DAY(Op_Fecha)) AS End
	FROM ordenespago
	WHERE (MONTH(Op_Fecha) = '{$ConsultMonth}' AND YEAR(Op_Fecha) = '{$ConsultYear}')
        GROUP BY MONTH(Op_Fecha) ");

        $Long = mktime(10, 0, 0, $ConsultMonth, 15, $ConsultYear);
        $Mes = date("M, Y", $Long);
        $PrintResults .= '<tr><td colspan="20">';
        $PrintResults .= '<div style="font-weight:bold;text-align:center;border:1px solid #CCC;margin:2px;padding:3px;background-color:#F2F2F2">Resumo Mensal de lucro por lojas do mês ' . $Mes;
        if (isset($Interv[1])) {
            $PrintResults .= ' (Até dia ' . $Interv[1]['End'] . ')';
        }
        $PrintResults .= '</div></td></tr>';
        $PrintResults .= $head;
        $TotalSumWorkers = 0;

        $QrStrDate[2] = "(MONTH(FraFecha) = '$ConsultMonth' AND YEAR(FraFecha) = '$ConsultYear')";
        $QrStrDateClient[2] = "(MONTH(CliFechaAlta) = '$ConsultMonth' AND YEAR(CliFechaAlta) = '$ConsultYear')";

    }
    if ($Country == 0) {
        $ExtendQuery = " UNION
	SELECT
	office_number AS RowID,
	0 AS SentAmount,
	0 AS PaidAmount,
	0 AS PaidCost,
	0 AS Fee,
	0 AS Prom,
	0 AS Counter,
	0 AS NewClients,
	main_offices.ref AS OfficeID,
	main_offices.name AS RowNAME,
	SUM(DISTINCT RsAmount+RsID)-SUM(DISTINCT RsID) AS GoalAmount,
	1 AS Members
	FROM systemoffice.main_offices
  	LEFT JOIN systemoffice.finantial_results ON RsType = IF($Country>0,1,2) $RsProfit AND RsDate BETWEEN '$DateStart 00:00:00' AND '$DateEnd 23:59:59' AND ref = RsOffice

 	 WHERE office_number = 800
	GROUP BY OfficeID ";
    }
    if($DateStart<'2010-03-16 00:00:00'){
       #Old
   $SQL[1] = "SELECT
	IF($QrOrigens=FraOrigen0
		,IF(FraOrigen0=822 AND FraFechaIngreso<'2010-03-16','8220',
				IF(FraOrigen0=814 AND OriTipo = 1 AND FraFechaIngreso<'2010-03-16','8140',FraOrigen0)),
	IF($QrOrigens=dom_Region,
		 IF(FraOrigen0 = 822 AND FraFechaIngreso<'2010-03-16','Grande Lisboa',
				IF(FraOrigen0=814 AND OriTipo = 1 AND FraFechaIngreso<'2010-03-16','Call Center Portugal',$QrOrigens)),
	$QrOrigens)) AS RowID,
	SUM($SentAmount[1]) AS SentAmount,
	SUM($PaidAmount[1]) AS PaidAmount,
	SUM(($PaidAmount[1])/$RateField) AS PaidCost,
	SUM($FeesAmount[1]) AS Fee,
	SUM(FraProm) AS Prom,
	SUM(IF(FraStatus IN (10,21),-1,1)) AS Counter,
	COUNT(DISTINCT CliNumero) AS NewClients,
	main_offices.ref AS OfficeID,
	IF($QrRowName=dom_Region,
		 IF(FraOrigen0 = 822 AND FraFechaIngreso<'2010-03-16','Grande Lisboa',
				IF(FraOrigen0=814 AND OriTipo = 1 AND FraFechaIngreso<'2010-03-16','Call Center Portugal',$QrRowName)),
	$QrRowName) AS RowNAME,
	SUM(DISTINCT RsAmount+RsID)-SUM(DISTINCT RsID) AS GoalAmount,
	IF(StaffMembers.Sum>1,StaffMembers.Sum,1) AS Members
	FROM ordenespago
	INNER JOIN ordenesfinanceiro ON Of_Orden = Op_Orden
	INNER JOIN facturas ON FraOrdenPago = Op_Orden
        LEFT JOIN beneficiarios ON BnfNumero = FraBnfNumero
	LEFT JOIN hd_Paises ON pai_Codelite = BnfPais
	LEFT JOIN clientes ON FraCliNumero = CliNumero AND $QrStrDateClient[$l]
	LEFT JOIN origenes ON OriNumero = FraOrigen1
	LEFT JOIN dominios ON OriDominio = dom_ID
	LEFT JOIN $__DBD.main_offices ON office_number = IF(FraOrigen0=822 AND FraFechaIngreso<'2010-03-16','8220',IF(FraOrigen0=814 AND OriTipo = 1 AND FraFechaIngreso<'2010-03-16',IF($QrOrigens=dom_Region,822,'8140'),FraOrigen0))
	LEFT JOIN $__DBD.finantial_results ON RsType = IF($Country>0,1,2) $RsProfit AND YEAR(RsDate) = YEAR(FraFecha) AND MONTH(RsDate) = MONTH(FraFecha) AND RsOri = IF(FraOrigen0=822 AND FraFechaIngreso<'2010-03-16','8220',IF(FraOrigen0=814 AND OriTipo = 1 AND FraFechaIngreso<'2010-03-16',IF($QrOrigens=dom_Region,822,'8140'),FraOrigen0))
	LEFT JOIN (SELECT RefOffice, Sum FROM (SELECT RefOffice, Sum FROM $__DBD.staff ORDER BY Time DESC) AS StaffList GROUP BY RefOffice) AS StaffMembers ON RefOffice = main_offices.ref
	WHERE FraID BETWEEN $GETMIN AND $GETMAX AND $QrStrDate[$l]
		AND FraStatus IN (7,8,9,10,11,21)
	AND $RateField > 0
	$QrCountry$QrChannel
	GROUP BY RowID";
$SQL[2] = "SELECT
	$QrOrigens AS RowID,
	SUM($SentAmount[2]) AS SentAmount,
	SUM($PaidAmount[2]) AS PaidAmount,
	SUM(($PaidAmount[2])/$RateField) AS PaidCost,
	SUM($FeesAmount[2]) AS Fee,
	SUM(FraProm) AS Prom,
	SUM(IF(FraStatus IN (10,21),-1,1)) AS Counter,
	COUNT(DISTINCT CliNumero) AS NewClients,
	main_offices.ref AS OfficeID,
	$QrRowName AS RowNAME,
	SUM(DISTINCT RsAmount+RsID)-SUM(DISTINCT RsID) AS GoalAmount,
	IF(StaffMembers.Sum>1,StaffMembers.Sum,1) AS Members
	FROM ordenespago
	INNER JOIN ordenesfinanceiro ON Of_Orden = Op_Orden
	INNER JOIN facturas_income ON FraIncOrdenPago = Op_Orden
	INNER JOIN facturas ON FraID = FraIncID
        INNER JOIN facturasextjoins ON FraExtID = FraID
	LEFT JOIN hd_Paises ON pai_Codelite = FraBnfPais
	LEFT JOIN clientes ON FraCliNumero = CliNumero AND $QrStrDateClient[$l]
	LEFT JOIN origenes ON OriNumero = FraOrigen1
	LEFT JOIN dominios ON OriDominio = dom_ID
	LEFT JOIN $__DBD.main_offices ON office_number = FraOrigen0
	LEFT JOIN $__DBD.finantial_results ON RsType = IF($Country>0,1,2) $RsProfit AND YEAR(RsDate) = YEAR(FraFecha) AND MONTH(RsDate) = MONTH(FraFecha) AND RsOri = FraOrigen0
	LEFT JOIN (SELECT RefOffice, Sum FROM (SELECT RefOffice, Sum FROM $__DBD.staff ORDER BY Time DESC) AS StaffList GROUP BY RefOffice) AS StaffMembers ON RefOffice = main_offices.ref
	WHERE FraID = 0
	GROUP BY RowID";

    $SQL[0] = $SQL[1]." UNION ".$SQL[2];
    } else {

    $SQL[1] = "SELECT
	$QrOrigens AS RowID,
	SUM($SentAmount[1]) AS SentAmount,
	SUM($PaidAmount[1]) AS PaidAmount,
	SUM(($PaidAmount[1])/$RateField) AS PaidCost,
	SUM($FeesAmount[1]) AS Fee,
	SUM(FraProm) AS Prom,
	SUM(IF(FraStatus IN (10,21),-1,1)) AS Counter,
	COUNT(DISTINCT CliNumero) AS NewClients,
	main_offices.ref AS OfficeID,
	$QrRowName AS RowNAME,
	SUM(DISTINCT RsAmount+RsID)-SUM(DISTINCT RsID) AS GoalAmount,
	IF(StaffMembers.Sum>1,StaffMembers.Sum,1) AS Members
	FROM ordenespago
	INNER JOIN ordenesfinanceiro ON Of_Orden = Op_Orden
	INNER JOIN facturas ON FraOrdenPago = Op_Orden
        INNER JOIN facturasextjoins ON FraExtID = FraID
	LEFT JOIN hd_Paises ON pai_Codelite = FraBnfPais
	LEFT JOIN clientes ON FraCliNumero = CliNumero AND $QrStrDateClient[$l]
	LEFT JOIN origenes ON OriNumero = FraOrigen1
	LEFT JOIN dominios ON OriDominio = dom_ID
	LEFT JOIN $__DBD.main_offices ON office_number = FraOrigen0
	LEFT JOIN $__DBD.finantial_results ON RsType = IF($Country>0,1,2) $RsProfit AND YEAR(RsDate) = YEAR(FraFecha) AND MONTH(RsDate) = MONTH(FraFecha) AND RsOri = FraOrigen0
	LEFT JOIN (SELECT RefOffice, Sum FROM (SELECT RefOffice, Sum FROM $__DBD.staff ORDER BY Time DESC) AS StaffList GROUP BY RefOffice) AS StaffMembers ON RefOffice = main_offices.ref
	WHERE FraID BETWEEN $GETMIN AND $GETMAX AND $QrStrDate[$l]
            AND FraStatus IN (7,8,9,10,11,21)
            AND $RateField > 0
            $QrCountry
            $QrChannel
	GROUP BY RowID";

    $SQL[2] = "SELECT
	$QrOrigens AS RowID,
	SUM($SentAmount[2]) AS SentAmount,
	SUM($PaidAmount[2]) AS PaidAmount,
	SUM(($PaidAmount[2])/$RateField) AS PaidCost,
	SUM($FeesAmount[2]) AS Fee,
	SUM(FraProm) AS Prom,
	SUM(IF(FraStatus IN (10,21),-1,1)) AS Counter,
	COUNT(DISTINCT CliNumero) AS NewClients,
	main_offices.ref AS OfficeID,
	$QrRowName AS RowNAME,
	SUM(DISTINCT RsAmount+RsID)-SUM(DISTINCT RsID) AS GoalAmount,
	IF(StaffMembers.Sum>1,StaffMembers.Sum,1) AS Members
	FROM ordenespago
	INNER JOIN ordenesfinanceiro ON Of_Orden = Op_Orden
	INNER JOIN facturas_income ON FraIncOrdenPago = Op_Orden
	INNER JOIN facturas ON FraID = FraIncID
        INNER JOIN facturasextjoins ON FraExtID = FraID
	LEFT JOIN hd_Paises ON pai_Codelite = FraBnfPais
	LEFT JOIN clientes ON FraCliNumero = CliNumero AND $QrStrDateClient[$l]
	LEFT JOIN origenes ON OriNumero = FraOrigen1
	LEFT JOIN dominios ON OriDominio = dom_ID
	LEFT JOIN $__DBD.main_offices ON office_number = FraOrigen0
	LEFT JOIN $__DBD.finantial_results ON RsType = IF($Country>0,1,2) $RsProfit AND YEAR(RsDate) = YEAR(FraFecha) AND MONTH(RsDate) = MONTH(FraFecha) AND RsOri = FraOrigen0
	LEFT JOIN (SELECT RefOffice, Sum FROM (SELECT RefOffice, Sum FROM $__DBD.staff ORDER BY Time DESC) AS StaffList GROUP BY RefOffice) AS StaffMembers ON RefOffice = main_offices.ref
	WHERE FraID BETWEEN $GETMIN AND $GETMAX AND $QrStrDate[$l]
            AND FraStatus IN (7,8,9,10,11,21)
            AND $RateField > 0
            $QrCountry
            $QrChannel
	GROUP BY RowID";

    $SQL[0] = $SQL[1]." UNION ".$SQL[2];
    }
    $GET = $GLOBALS['MYT']->db_array($SQL[$Channel]." $ExtendQuery ORDER BY RowID");
    $CGET = $GET[0] + 1;
    if (!isset($GET[1])) {
        $PrintResults .= $NoResults;
    } else {
        for ($h = 1; $h <= $CGET; $h++) {
            if (!isset($GET[$h]['GoalAmount'])) {$GET[$h]['GoalAmount'] = 0;}
            if (FieldResult($GET, $h, 'GoalAmount') > 0) {
                $PercentGoal = FieldResult($GET, $h, 'SentAmount') / FieldResult($GET, $h, 'GoalAmount') * 100;
            } else {
                $PercentGoal = '0';
            }

            if ($Country == 2) {
                $REL = FieldResult($GET, $h, 'SentAmount') / 100;
                $GET[$h]['imp'] = $REL * 0.12;
                $GET[$h]['COMM'] = FieldResult($GET, $h, 'Fee') - FieldResult($GET, $h, 'imp');
                $GET[$h]['COMM1'] = ((FieldResult($GET, $h, 'COMM') / 3) * 1);
                $GET[$h]['Fee'] = FieldResult($GET, $h, 'COMM1');
                $GET[$h]['COMM2'] = ((FieldResult($GET, $h, 'COMM') / 3) * 2);
            }

            if (FieldResult($GET, $h, 'SentAmount') > 0) {
                $RateIN = FieldResult($GET, $h, 'PaidAmount') / FieldResult($GET, $h, 'SentAmount');
            } else {
                $RateIN = 0;
            }
            if (FieldResult($GET, $h, 'SentAmount') > 0) {
                $RateOUT = FieldResult($GET, $h, 'PaidAmount') / FieldResult($GET, $h, 'PaidCost');
            } else {
                $RateOUT = 0;
            }
            $ProfitAmount = FieldResult($GET, $h, 'SentAmount') - FieldResult($GET, $h, 'PaidCost');
            if (FieldResult($GET, $h, 'SentAmount') > 0) {
                $Profit = ($ProfitAmount * 100) / FieldResult($GET, $h, 'SentAmount');
            } else {
                $Profit = 0;
            }
            if (FieldResult($GET, $h, 'Counter') > 0) {
                $Avg = FieldResult($GET, $h, 'SentAmount') / FieldResult($GET, $h, 'Counter');
                $AvgPft = $ProfitAmount / FieldResult($GET, $h, 'Counter');
                $AvgFee = FieldResult($GET, $h, 'Fee') / FieldResult($GET, $h, 'Counter');
            } else {
                $Avg = 0;
                $AvgPft = 0;
                $AvgFee = 0;
            }
            $PrintResults .= '<tr bgcolor="';
            if ($h < $CGET) {
                $PrintResults .= gridlines($h);
            } else {
                $PrintResults .= '#CCCCCC';
            }
            $PrintResults .= '" height="18">
	<td NOWRAP style="padding:1px;border-bottom:1px solid #BBB;">';
            if ($h != $CGET) {
                $PrintResults .= $GET[$h]['RowNAME'];
            } else {
                $PrintResults .= 'Totais: ';
            }
            $PrintResults .= '</td>
	<td align="right" style="padding-left:10px;padding-right:2px;border-bottom:1px solid #BBB;border-left:1px solid #777;">' . FieldResult($GET, $h, 'Counter') . '</td>
	<td align="right" style="padding-left:10px;padding-right:2px;border-bottom:1px solid #BBB;border-left:1px solid #777;">' . number_format(FieldResult($GET, $h, 'SentAmount'), 2, ',', '.') . $currency . '</td>
	<td align="right" style="padding-left:10px;padding-right:2px;border-bottom:1px solid #BBB;border-left:1px solid #CCC;">';

            if ($Country == 2) {
                $ProfitAmount = $ProfitAmount + FieldResult($GET, $h, 'Fee');
                $Profit = $Profit + FieldResult($GET, $h, 'Fee');

                $PrintResults .= number_format(FieldResult($GET, $h, 'COMM1'), 2, ',', '.') . $currency . '</td>
	<td align="right" style="padding-left:10px;padding-right:2px;border-bottom:1px solid #BBB;border-left:1px solid #CCC;">' . number_format(FieldResult($GET, $h, 'COMM2'), 2, ',', '.') . $currency . '</td>
	<td align="right" style="padding-left:10px;padding-right:2px;border-bottom:1px solid #BBB;border-left:1px solid #CCC;">' . number_format(FieldResult($GET, $h, 'imp'), 2, ',', '.') . $currency;
            } else {
                $PrintResults .= number_format((FieldResult($GET, $h, 'Fee')), 2, ',', '.') . $currency . '</td>
	<td align="right" style="padding-left:10px;padding-right:2px;border-bottom:1px solid #BBB;border-left:1px solid #CCC;">';
                $PrintResults .= number_format((FieldResult($GET, $h, 'Prom')), 2, ',', '.') . $currency;
            }
            $PrintResults .= '</td>
	<td align="right" style="padding-left:10px;padding-right:2px;border-bottom:1px solid #BBB;border-left:1px solid #777;" NOWRAP>' . number_format($ProfitAmount, 2, ',', '.') . $currency . '</td>
	<td align="right" style="padding-left:10px;padding-right:2px;border-bottom:1px solid #BBB;border-left:1px solid #CCC;" NOWRAP>' . number_format($ProfitAmount + FieldResult($GET, $h, 'Fee'), 2, ',', '.') . $currency . '</td>
	<td align="right" style="padding-left:10px;padding-right:2px;border-bottom:1px solid #BBB;border-left:1px solid #CCC;" NOWRAP>' . number_format($Profit, 1, ',', '.') . ' %</td>
	<td align="right" style="padding-left:10px;padding-right:2px;border-bottom:1px solid #BBB;border-left:1px solid #777;" NOWRAP>' . number_format($AvgFee, 2, ',', '.') . $currency . '</td>
	<td align="right" style="padding-left:10px;padding-right:2px;border-bottom:1px solid #BBB;border-left:1px solid #CCC;" NOWRAP>' . number_format($AvgPft, 2, ',', '.') . $currency . '</td>
	<td align="right" style="padding-left:10px;padding-right:2px;border-bottom:1px solid #BBB;border-left:1px solid #CCC;" NOWRAP>' . number_format($Avg, 2, ',', '.') . $currency . '</td>
	<td align="right" style="padding-left:10px;padding-right:2px;border-bottom:1px solid #BBB;border-left:1px solid #777;" NOWRAP>' . number_format(FieldResult($GET, $h, 'Counter') / FieldResult($GET, $h, 'Members'), 1) . '</td>
	<td align="right" style="padding-left:10px;padding-right:2px;border-bottom:1px solid #BBB;border-left:1px solid #777;" NOWRAP>' . FieldResult($GET, $h, 'NewClients') . '</td>';
            if ($Country == 0) {
                $PrintResults .= '<td align="right" style="padding-left:10px;padding-right:2px;border-bottom:1px solid #BBB;border-left:1px solid #777;" NOWRAP>' . number_format(FieldResult($GET, $h, 'GoalAmount'), 2, ',', '.') . '</td>
	<td align="right" style="padding-left:10px;padding-right:2px;border-bottom:1px solid #BBB;border-left:1px solid #CCC;" NOWRAP>' . number_format($ProfitAmount + FieldResult($GET, $h, 'Fee') - FieldResult($GET, $h, 'GoalAmount'), 2, ',', '.') . '</td>';
            } else {
                $PrintResults .= '<td align="right" style="padding-left:10px;padding-right:2px;border-bottom:1px solid #BBB;border-left:1px solid #777;" NOWRAP>' . number_format(FieldResult($GET, $h, 'GoalAmount'), 2, ',', '.') . '</td>
	<td align="right" style="padding-left:10px;padding-right:2px;border-bottom:1px solid #BBB;border-left:1px solid #CCC;" NOWRAP>' . number_format(FieldResult($GET, $h, 'SentAmount') - FieldResult($GET, $h, 'GoalAmount'), 2, ',', '.') . '</td>
	<td align="right" style="padding-left:10px;padding-right:2px;border-bottom:1px solid #BBB;border-left:1px solid #CCC;" NOWRAP>' . number_format($PercentGoal, 1, ',', '.') . ' %</td>';
            }
            $PrintResults .= '</tr>';
        }
    }
}

$PrintResults .= '</table></div>';
echo $PrintResults; ?>